<?php
header("Content-Type:application/json");

try {
    $pdo = new PDO("mysql:host=localhost;dbname=linux", "root", "linux2016");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->query("SET NAMES UTF8");
} catch (PDOException $e) {
    echo '连接数据库失败' . $e->getMessage();
    exit;
}

$i = 0;
$data['from'] = 'PHP';
foreach ($pdo->query("select * from mock") as $row){
  $data['list'][$i]['username']=$row['username'];
  $data['list'][$i]['phone']=$row['phone'];
  $data['list'][$i]['grade']=$row['grade'];
  $data['list'][$i]['school']=$row['school'];
  $i++;
}
$data = json_encode($data);
echo $data;

?>
